import router from '@/router';
import store from '@/store';
import NProgress from 'nprogress';
import { Message } from 'element-ui';
import lang from '@/lang';


const permissionAfterLogin = (to, from, next) => {
  // 登陆后无子账号去子账户页添加
  if (!store.getters.subCuid && to.meta.needSubAccount) {
    next({
      path: '/subaccount',
      query: to.query,
    });
    NProgress.done();
  // type!==3 不能进入邀请
  } else if (store.getters.type !== 3 && to.path === '/invite') {
    next({
      path: '/',
      query: to.query,
    });
  } else {
    next();
  }
};

// 权限判断
const authority = (to, from, next) => {
  if (to.query.access_key) {
    if (to.query.access_key === store.getters.obAccessKey) {
      next({
        path: to.meta.observer ? undefined : '/pools/dashboard',
        query: to.query,
      });
    } else {
      store.dispatch('AccessKey', to.query.access_key).then(() => {
        next({
          path: to.meta.observer ? undefined : '/pools/dashboard',
          query: to.query,
        });
      }).catch(() => {
        delete Object.assign(to.query).access_key;
        next({
          path: to.meta.freeLogin ? to.path : '/',
          query: to.query,
        });
      });
    }
  } else if (store.getters.id) {
    permissionAfterLogin(to, from, next);
  } else if (store.getters.isMainInfo) {
    next({
      path: to.meta.freeLogin ? undefined : '/login',
      query: to.meta.freeLogin ? to.query : Object.assign(to.query, { from_url: to.path }),
    });
  } else {
    store.dispatch('MainInfo').then(() => {
      if (store.getters.id) {
        permissionAfterLogin(to, from, next);
      } else {
        next({
          path: to.meta.freeLogin ? undefined : '/login',
          query: to.meta.freeLogin ? to.query : Object.assign(to.query, { from_url: to.path }),
        });
      }
    }).catch(() => {
      Message.error(lang.t('error.catchError'));
    });
  }
};

router.beforeEach((to, from, next) => {
  NProgress.start();
  if ((to.query.type === 'buc' || to.query.type === 'mut') && (to.path === '/wallet/my' || to.path === '/wallet/bill' || to.path === '/wallet/forward' || to.path === '/wallet/forward')) {
    next('/404');
  }
  if ((from.query.invite_from || from.query.t) && !(to.query.invite_from || to.query.t)) {
    next({
      path: to.path,
      query: Object.assign(to.query, { t: from.query.t, invite_from: from.query.invite_from }),
    });
  }
  if (from.query.access_key && from.query.access_key === store.getters.obAccessKey && !to.query.access_key) {
    next({
      path: to.meta.observer ? to.path : '/pools/dashboard',
      query: Object.assign(to.query, { access_key: from.query.access_key }),
    });
  }
  if (to.query.access_key !== store.getters.obAccessKey) {
    store.commit('CLEAR_ALL');
  }
  // if (!store.getters.token || !store.getters.dcrToken) {
  //   Promise.all([store.dispatch('Auth'), store.dispatch('DcrAuth')]).then(() => {
  //     authority(to, from, next);
  //   }).catch(() => {
  //     next({
  //       path: to.path,
  //       query: to.query,
  //     });
  //   });
  // } else {
  //   authority(to, from, next);
  // }
  authority(to, from, next);
});

router.afterEach((to) => {
  window.document.title = lang.t(to.meta.title);
  if (to.path) {
    /* eslint-disable no-underscore-dangle */
    window._hmt.push(['_trackPageview', to.fullPath]);
  }
  NProgress.done();
});
